package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/10.
 * yang-wen-wen
 */

public class DynamicsDetailsActivity extends BaseActivity {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.picture)
    ImageView picture;
    @BindView(R.id.web_view)
    WebView webView;
    private  String articleId,userid;
    private int type;

    @Override
    protected int setLayout() {
        return R.layout.activity_dynamics_details;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        showLoading("");
        headline.setText("资讯详情");
        loadImageCtrl = new LoadImageCtrl(this);
        Bundle extras = getIntent().getExtras();
        String from = extras.getString(Constant.FROM);
        articleId=extras.getString(Constant.MESSAGE_ID);
        type=extras.getInt(Constant.MESSAGE_TYPE);
        userid= DaoManager.getInstance().getUserBean().getId()+"";

        if (!TextUtils.isEmpty(from)) {
            MessageModel model = extras.getParcelable(Constant.DATA);
            //title.setText(model.getTitle());
            //time.setText(model.getCreateTime());
            //content.setText(model.getMessage());
            //picture.setVisibility(View.GONE);
            setData(model.getContent());
        } else {
            MessageModel model = extras.getParcelable(Constant.DATA);
            //title.setText(model.getTitle());
            //time.setText(model.getCreateTime());
           // content.setText(model.getContent());
            if(null!=model)
            setData(model.getContent());
            //loadImageCtrl.getGlide().load(model.getPicTure()).into(picture);
        }

        if(3==type) {
            shareReceiver();
        }

        stopLoading();
    }

    private void shareReceiver() {
        ApiCreator.getApi().getMessage(articleId,userid)
                .compose(RxTransformer.<MessageModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MessageModel>() {
                    @Override
                    protected void onSuccess(MessageModel model) {
                        setData(model.getContent());
                    }

                    @Override
                    public void onFinish() {
                        //  ToastUtil.showToast("服务器异常!");
                    }
                });


    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return false;
    }


    private void setData(String data) {
        webView.loadData(data,"text/html; charset=UTF-8", null);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
            }
        });

        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setDomStorageEnabled(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 自适应屏幕
//        webView.getSettings().setUseWideViewPort(true);//让webview读取网页设置的viewport，pc版网页 
//        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        registerForContextMenu(webView);

    }


}
